/*
 Copyright (c) 2009 by contributors:

 * James Hight (http://labs.zavoo.com/)
 * Richard R. Masters

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

package;

	import flash.Boot;
	import flash.display.DisplayObject;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.Lib;
	import flash.text.TextField;
	import flash.xml.XML;
	import haxe.Stack;
	import me.cunity.debug.Out;
	import org.svgweb.events.SVGEvent;
    import org.svgweb.SVGViewerFlash;
    import flash.display.Sprite;
	
	class SVGActionViewer extends SVGViewerFlash
	{
		public function new() { super(); }
		override public function addActionListener(eventType:String, target:EventDispatcher):Void 
		//override public function addActionListener(eventType:String, target:SVGSprite):Void 
		{
			untyped { trace(target.svgNode.id + 'adding listener...' + target.svgNode); }
			Out.dumpStack(Stack.callStack());
			target.addEventListener(MouseEvent.CLICK, onClickHandler);
		}
		
		function onClickHandler(evt:MouseEvent)
		{
			trace(evt.currentTarget == evt.target ? 'true':'false');
			trace(evt.target.svgNode.id + 'svgNode:' + evt.target.svgNode);
			try {
				trace('svgParent:' + evt.target.svgNode.svgParent);
				trace('svgRoot:' + evt.target.svgNode.svgRoot);
				trace('xml:' + evt.target.svgNode.xml);
			}
			catch (ex:Dynamic)
			{
				trace(ex);
			}
		}
	}
    
    class Test extends Sprite {
        
        //var svgImage:SVGViewerFlash;
        var svgImage:SVGActionViewer;
		public var v:DisplayObject;

        public function new()
        {
            super();
            //svgImage = new SVGViewerFlash();
            svgImage = new SVGActionViewer();
            v = this.addChild(svgImage);
		}
		public function load()
		{
            svgImage.xml = new XML('<svg version="1.1" baseProfile="tiny"' +
                                   'xmlns="http://www.w3.org/2000/svg"' +
                                   'xmlns:xlink="http://www.w3.org/1999/xlink"' +
                                   'width="500" height="500" viewBox="0 0 500 500">' +
                                   '<rect id="test" x="100" y="100" width="300" height="300" style="fill:red" onclick="test" /></svg>');

        }
		
		public static function main()
		{
			var t = new Test();
			var tt:TextField = Boot.getTrace();
			tt.x = 400;
			//t.x = 400;
			t.graphics.beginFill(0x5a5fa3);
			t.graphics.drawRect(0, 0, 400, 400);
			var d:DisplayObject = Lib.current.addChild(t);
			t.load();
			Out.dumpLayout(t.v);
			Out.dumpLayout(d);
		}		
        
    }
	

